// *** Movement Object *** //
// Maps keyboard to player movement and interactions
//  
// @project: GoldenShield
// @author: Anders Ipsen
// @version: 1.0

var movementObject = {
    collideSide: {},        //Holds collsion site
    collideObject: {},      //Holds the collided object
    collide: {},            //Holds the collision detection object
    keyArrowUp: false,      //Boolean keyUp value
    keyArrowDown: false,    //Boolean keyDown value
    keyArrowLeft: false,    //Boolean KeyArrowLeft value
    keyArrowRight: false,   //Boolean keyDown value
    wKeyUp: false,          //Boolean wKeyUp value
    sKeyDown: false,        //Boolean sKeyDown value
    aKeyLeft: false,        //Boolean aKeyLeft value
    dKeyRight: false,       //Boolean dKeyRight value
    space: false,           //Boolean space value
    stopPlyaer: false,      //Bolean player stop value, if true movement stops
    
    //Runs when a keydown event accurs
    onKeyDown: function(e) {
        switch (e.keyCode)
        { 
        // :::: FOR WASD MOVEMENT ::::
        case 65:    //left
            this.aKeyLeft = true;  
            break;    
            
        case 68:    //right
            this.dKeyRight = true;     
            break;
            
        case 87:    //up
            this.wKeyUp = true; 
            break;
            
        case 83:    //down
            this.sKeyDown = true; 
            break;
            
        // :::: FOR ARROW MOVEMENT ::::
        
        case 37:    //left
            this.keyArrowLeft = true;  
            break; 
            
        case 39:    //right
            this.keyArrowRight = true;    
            break;
            
        case 38:    //up
            this.keyArrowUp = true;
            break;
            
            
        case 40:    //down
            this.keyArrowDown = true;
            break;
            
        case 13:    //enter            
            //If the game hasn't been started, start it!
            if(!scene.gameStart) {
                scene.start();
            } else {
                if(character.showObject) {
                    character.showQuest(character.showObject,true);
                } else {
                    guiObject.removeMessage();
                } 
                
                if(scene.gameIsOver) {
                    scene.continueFromGameOver();
                }
            }
            
            if(site.boxShown) {
                site.removeBox();
            }
            
        break;
        case 32: //Space
        if(!character.questIsShowing) {
            if(!this.space) {
                this.space = true;
                player.hitEnemy();
            }
        }
        break;
        } 
    },
    
    //Runs when a keyUp event accurs
    onKeyUp: function(e) {
                switch (e.keyCode)
        { 
        // :::: FOR WASD MOVEMENT ::::
        case 65:    //left
            this.aKeyLeft = false;  
            break;    
            
        case 68:    //right
            this.dKeyRight = false;     
            break;
            
        case 87:    //up
            this.wKeyUp = false; 
            break;
            
        case 83:    //down
            this.sKeyDown = false; 
            break;
            
        // :::: FOR ARROW MOVEMENT ::::
        
        case 37:    //left
            this.keyArrowLeft = false;  
            break; 
            
        case 39:    //right
            this.keyArrowRight = false;    
            break;
            
        case 38:    //up
            this.keyArrowUp = false;
            break;
            
        case 40:    //down
            this.keyArrowDown = false;
            break;
        case 32: //Space
            this.space = false;
        break;
        }
    },
    update: function() {
       if(!movementObject.stopPlayer) {
        if(!movementObject.space) {
            if(!character.questIsShowing) {
        if (movementObject.keyArrowUp) {               //ARROW KEYS
            if(!movementObject.collideSide.bottom) {
                player.moveUp();
            }
        } else if (movementObject.keyArrowDown) {
            if(!movementObject.collideSide.top) {
                player.moveDown();
            }
        } else if (movementObject.keyArrowLeft) {
            if(!movementObject.collideSide.left) {
                player.moveLeft();
            }
        } else if (movementObject.keyArrowRight) {
            if(!movementObject.collideSide.right) {
                player.moveRight();
            }}
        else if (movementObject.wKeyUp) {               //WASD
            if(!movementObject.collideSide.bottom) {
                player.moveUp();
            }
        } else if (movementObject.sKeyDown) {
            if(!movementObject.collideSide.top) {
                player.moveDown();
            }
        } else if (movementObject.aKeyLeft) {
            if(!movementObject.collideSide.left) {
                player.moveLeft();
            }
        } else if (movementObject.dKeyRight) {
            if(!movementObject.collideSide.right) {
                player.moveRight();
            }            
        }               //COLLISION
        movementObject.collide = collision.detect(player);
        movementObject.collideSide = movementObject.collide.collisionType;
        movementObject.collideObject = movementObject.collide.collisionObject;
       }
       }
       }
    },
    stop: function() {
        this.stopPlayer = true;
    },
    
    start: function() {
        this.stopPlayer = false;
    } 
}